package com.gel.aiagent.database;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
/*多数据源配置*/
@Configuration
public class MyDataSource {
    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.mysql")
    public DataSourceProperties mysqlProperties(){
        return  new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource mysqlDataSource(){
        return mysqlProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    @Primary
    public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean
    @ConfigurationProperties("spring.datasource.pgsql")
    public DataSourceProperties postgresProperties(){
        return  new DataSourceProperties();
    }

    @Bean
    public DataSource postgresDataSource(){
        return postgresProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
}
